﻿using Application.Common.Interfaces;
using Application.Features.Organizations.DTOs;
using Application.Features.Organizations.Specifications;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Common.Attributes;
using Common.Extensions;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace Application.Features.Organizations.Queries.GetAll;

/// <summary>
/// 查询全部组织机构
/// </summary>
[Description("全部查询")]
[GroupDescription("组织机构")]
public class GetAllOrganizationsQuery : IRequest<IEnumerable<OrganizationOutput>>
{
}
public class GetAllDepartmentsQueryHandler :
IRequestHandler<GetAllOrganizationsQuery, IEnumerable<OrganizationOutput>>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;
    public GetAllDepartmentsQueryHandler(
    IApplicationDbContext context,
        IMapper mapper
    )
    {
        _context = context;
        _mapper = mapper;
    }

    public async Task<IEnumerable<OrganizationOutput>> Handle(GetAllOrganizationsQuery request, CancellationToken cancellationToken)
    {
        return await _context.Organizations
            .OrderByDescending(x => x.Sort)
            .ProjectTo<OrganizationOutput>((_mapper.ConfigurationProvider))
            .ToListAsync();
    }
}